//
//  WiteBoxTestsViewController.m
//  WiteBox
//
//  Created by John Blanco on 6/28/09.
//  Copyright 2009 Effective UI. All rights reserved.
//

#import "WiteBoxTestsViewController.h"

#import "TestRun.h"
#import "TestCaseRun.h"
#import "AssertionException.h"
#import "WiteBoxStatisticsViewController.h"
#import "WiteBoxStyles.h"
#import "TestLocationModel.h"

@implementation WiteBoxTestsViewController

@synthesize witeBoxStatisticsViewController;

/*
 - (id)initWithStyle:(UITableViewStyle)style {
 // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
 if (self = [super initWithStyle:style]) {
 }
 return self;
 }
 */

- (void)viewDidLoad {
    [super viewDidLoad];
	
	// model
	testLocationModel = [TestLocationModel sharedInstance];
	
	self.title = [NSString stringWithFormat:@"%@ v%@", [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleName"], [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleVersion"]];
	self.view.backgroundColor = [WiteBoxStyles splitBackgroundColor];
	self.navigationItem.backBarButtonItem =
	[[UIBarButtonItem alloc] initWithTitle:@"Back"
									 style: UIBarButtonItemStyleBordered
									target:nil
									action:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
	[self.tableView reloadData];
}

/*
 - (void)viewDidAppear:(BOOL)animated {
 [super viewDidAppear:animated];
 }
 */
/*
 - (void)viewWillDisappear:(BOOL)animated {
 [super viewWillDisappear:animated];
 }
 */
/*
 - (void)viewDidDisappear:(BOOL)animated {
 [super viewDidDisappear:animated];
 }
 */

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	return YES;
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */


/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
 }   
 else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }   
 }
 */


/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */


/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */


- (void)dealloc {
	[witeBoxStatisticsViewController release];
    [super dealloc];
}

#pragma mark -

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [testLocationModel testCaseRuns].count + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"TestCell";
	
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
		cell.textLabel.font = [UIFont systemFontOfSize:14.0];
		cell.detailTextLabel.font = [UIFont systemFontOfSize:12.0];
    }
	
	cell.backgroundColor = [WiteBoxStyles cellBackgroundColor];
	cell.textLabel.textColor = [WiteBoxStyles cellTextColor];

	if (indexPath.row == 0) {
		cell.textLabel.text = @"All";
	} else {
		TestCaseRun *testCaseRun = (TestCaseRun *)[[testLocationModel testCaseRuns] objectAtIndex:indexPath.row - 1]; 
		
		cell.textLabel.text = [testCaseRun testCaseClassName];
	}

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	if (indexPath.row == 0) {
		// show all
		witeBoxStatisticsViewController.testCaseRunToDisplay = nil;
	} else {
		// show a specific test case
		witeBoxStatisticsViewController.testCaseRunToDisplay = (TestCaseRun *)[[testLocationModel testCaseRuns] objectAtIndex:indexPath.row - 1];;
	}
}

#pragma mark -

- (void)updateDisplay {
	[self.tableView reloadData];
}

@end

